<template>
  <div>
    <div v-if="Show == 0">
      <div class="mlt">
        <el-form class="demo-form-inline" :inline="true" :model="form">
          <el-form-item label="家庭组" prop="region">
            <el-select
              v-model="form.group_id"
              filterable
              placeholder="请选家庭组"
              style="margin: 2px"
              @change="FamilyChange"
            >
              <el-option
                v-for="item in FamilyOptions"
                :key="item.group_id"
                :label="item.group_name"
                :value="item.group_id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="学员姓名" prop="region">
            <el-select
              v-model="form.member_id"
              filterable
              placeholder="请选学员姓名"
              style="margin: 2px"
            >
              <el-option
                v-for="item in userOptions"
                :key="item.id"
                :label="item.nick_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="老师姓名" prop="region">
            <el-select
              v-model="form.user_id"
              filterable
              placeholder="请选老师姓名"
              style="margin: 2px"
            >
              <el-option
                v-for="item in realOptions"
                :key="item.id"
                :label="item.real_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="课程" prop="region">
            <el-select
              v-model="form.goods_id"
              filterable
              placeholder="请选商品"
              style="margin: 2px"
            >
              <el-option
                v-for="items in commodityOptions"
                :key="items.goods_id"
                :label="items.goods_name"
                :value="items.goods_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="月份">
            <el-date-picker
              v-model="form.start_date"
              placeholder="选择月"
              type="month"
              value-format="yyyy-MM"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="init()">查询</el-button>
          </el-form-item>
        </el-form>
        <el-form class="demo-form-inline" :inline="true">
          <el-form-item>
            <el-button type="primary" @click="addLesson">添加备课</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="日期" prop="res_date">
          <template slot-scope="scoped">
            {{ scoped.row.start_date }} - {{ scoped.row.end_date }}
          </template>
        </el-table-column>
        <el-table-column label="审核时间" prop="updated_at" />
        <el-table-column label="状态" prop="status_name" />
        <el-table-column label="课程" prop="goods_name" />
        <el-table-column label="学员" prop="member_name" />
        <el-table-column label="备课人" prop="user_name" />
        <el-table-column label="操作" prop="">
          <template slot-scope="scoped">
            <el-button
              v-if="scoped.row.status == 'N'"
              type="primary"
              @click="examine(scoped.row)"
            >
              审核
            </el-button>
            <el-button
              v-if="scoped.row.status == 'N'"
              type="primary"
              @click="onSubmit(scoped.row)"
            >
              修改
            </el-button>
            <el-button
              v-if="scoped.row.status == 'Y'"
              type="primary"
              @click="onSubmit(scoped.row)"
            >
              查看
            </el-button>
            <br />
            <el-button
              style="margin-top: 10px"
              type="primary"
              @click="onresult(scoped.row)"
            >
              评估管理
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-if="total >= 10"
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </div>
    <div v-if="Show == 1">
      <AddLessons :datas="datas" @showMain="showMain" />
    </div>
    <div v-if="Show == 2">
      <result :datas="datas" @showMain="showMain" />
    </div>
    <div v-if="Show == 3">
      <See :datas="datas" @showMain="showMain" />
    </div>
  </div>
</template>

<script>
  // customerCaseDetail
  import { prepareList, checkPrepare, searchUser } from '@/api/edu'
  import { getCusGroup } from '@/api/gustomerGroup'
  import { getGoodsList } from '@/api/public'

  //
  import AddLessons from './components/addLessons.vue'
  import result from './components/result.vue'
  import See from './components/see.vue'

  export default {
    components: { AddLessons, result, See },
    data() {
      return {
        dialogVisible: false,
        form: {
          page: 1,
          pagesize: 10,
          user_id: '',
          group_id: '',
          member_id: '',
          goods_id: '',
          start_date: '',
        },
        formInline: {
          user: '',
          region: '',
        },

        total: '',
        Show: 0,
        tableData: [],
        datas: {
          id: '',
        },
        FamilyOptions: [],
        userOptions: [],
        commodityOptions: [],
        realOptions: [],
      }
    },
    mounted() {
      this.init()
    },
    methods: {
      init() {
        const data = {
          goods_type: 'curriculum',
        }
        getGoodsList(data).then((res) => {
          // 商品 list
          this.commodityOptions = res.data
        })
        searchUser().then((e) => {
          this.realOptions = e.data
        })
        getCusGroup().then((res) => {
          // 家庭组 list
          this.FamilyOptions = res.data
          if (this.datas.group_id) {
            res.data.forEach((e) => {
              if (e.group_id == this.datas.group_id) {
                this.userOptions = e.child
              }
            })
          }
        })
        prepareList(this.form).then((e) => {
          this.total = e.data.total
          this.tableData = e.data.data
        })
      },
      onSubmit(e) {
        if (e.status == 'Y') {
          //
          this.datas = e
          this.Show = 3
        } else {
          this.datas = e
          this.Show = 1
        }
      },
      onresult(e) {
        this.datas = e
        this.Show = 2
      },

      FamilyChange(e) {
        this.FamilyOptions.forEach((res) => {
          if (res.group_id == e) {
            this.userOptions = res.child
          }
        })
      },

      examine(e) {
        //审核
        console.log(e)
        this.$confirm('此操作将备课审核是否通过?', '提示', {
          confirmButtonText: '通过',
          cancelButtonText: '不通过',
          type: 'warning',
        })
          .then(() => {
            const data = {
              id: e.id,
              status: 'Y',
            }
            checkPrepare(data).then(() => {
              this.init()
              this.$message({
                type: 'success',
                message: '通过成功!',
              })
            })
          })
          .catch(() => {
            const data = {
              id: e.id,
              status: 'N',
            }
            checkPrepare(data).then(() => {
              this.init()

              this.$message({
                type: 'info',
                message: '不通过',
              })
            })
          })
      },
      showMain() {
        this.Show = 0
        this.init()
      },

      go() {
        this.init()
        this.Show = 0
      },
      addLesson() {
        this.Show = 1
        this.datas = {}
      },
      // 下一页
      handleCurrentChange(val) {
        const _this = this
        _this.form.page = val
        this.init()
      },
    },
  }
</script>

<style></style>
